package com.woniu.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniu.been.entity.CourseCategories;
import com.woniu.been.entity.CourseChapters;
import com.woniu.been.entity.Courses;
import com.woniu.been.entity.vo.CourseTreeVO;
import com.woniu.mapper.CourseCategoriesMapper;
import com.woniu.service.CourseCategoriesService;
import com.woniu.service.CourseChaptersService;
import com.woniu.service.CoursesService;

@Service
public class CourseCategoriesImpl implements CourseCategoriesService {
    @Autowired
    CourseCategoriesMapper courseCategoriesMapper;
    
    @Autowired
    CourseChaptersService courseChaptersService;
    
    @Autowired
    CoursesService coursesService;

    @Override
    public List<CourseCategories> getMainMenu(CourseCategories courseCategories) {
        return courseCategoriesMapper.selectCourse(courseCategories);
    }
    
    @Override
    public List<CourseCategories> getAllTopLevelCategories() {
        return courseCategoriesMapper.findAllCategories();
    }

    @Override
    public void addCategory(CourseCategories category) {
        courseCategoriesMapper.insertCategory(category);
    }

    @Override
    public void updateCategory(CourseCategories category) {
        courseCategoriesMapper.updateCategory(category);
    }

    @Override
    public void deleteCategory(Integer id) {
        courseCategoriesMapper.deleteCategory(id);
    }

    @Override
    public List<CourseCategories> getSubCategoriesByParentName(String parentCategoryName) {
        Integer parentCategoryId = courseCategoriesMapper.findCategoryIdByName(parentCategoryName);
        if (parentCategoryId != null) {
            return courseCategoriesMapper.findSubCategoriesByParentId(parentCategoryId);
        } else {
            return List.of();
        }
    }

    @Override
    public CourseCategories getCategoryById(Integer categoryId) {
        return courseCategoriesMapper.findById(categoryId);
    }
    
    @Override
    public List<CourseCategories> getSubCategoriesByParentId(Integer parentCategoryId) {
        return courseCategoriesMapper.findSubCategoriesByParentId(parentCategoryId);
    }
    
    @Override
    public Map<String, Object> getCompleteCourseTree() {
        Map<String, Object> courseTree = new HashMap<>();
        List<Map<String, Object>> categories = new ArrayList<>();
        
        // 获取所有课程大类（parent_category_id为null）
        List<CourseCategories> mainCategories = courseCategoriesMapper.findAllCategories();
        
        for (CourseCategories mainCategory : mainCategories) {
            if (mainCategory.getParentCategoryId() == null) {
                Map<String, Object> categoryNode = new HashMap<>();
                categoryNode.put("categoryId", mainCategory.getCategoryId());
                categoryNode.put("categoryName", mainCategory.getCategoryName());
                categoryNode.put("imgSrc", mainCategory.getImgSrc());
                
                // 获取该课程大类下的课程小类
                List<CourseCategories> subCategories = courseCategoriesMapper.findSubCategoriesByParentId(mainCategory.getCategoryId());
                List<Map<String, Object>> subCategoryNodes = new ArrayList<>();
                
                for (CourseCategories subCategory : subCategories) {
                    Map<String, Object> subCategoryNode = new HashMap<>();
                    subCategoryNode.put("categoryId", subCategory.getCategoryId());
                    subCategoryNode.put("categoryName", subCategory.getCategoryName());
                    subCategoryNode.put("imgSrc", subCategory.getImgSrc());
                    
                    // 获取该课程小类下的章节
                    List<CourseChapters> chapters = courseChaptersService.getChaptersBySubCategoryId(subCategory.getCategoryId());
                    List<Map<String, Object>> chapterNodes = new ArrayList<>();
                    
                    for (CourseChapters chapter : chapters) {
                        Map<String, Object> chapterNode = new HashMap<>();
                        chapterNode.put("chapterId", chapter.getChapterId());
                        chapterNode.put("chapterName", chapter.getChapterName());
                        chapterNode.put("isFree", chapter.getIsFree());
                        
                        // 获取该章节下的具体课程
                        List<Courses> courses = coursesService.getCoursesByChapterId(chapter.getChapterId());
                        List<Map<String, Object>> courseNodes = new ArrayList<>();
                        
                        for (Courses course : courses) {
                            Map<String, Object> courseNode = new HashMap<>();
                            courseNode.put("courseId", course.getCourseId());
                            courseNode.put("courseName", course.getCourseName());
                            courseNode.put("courseType", course.getCourseType());
                            courseNode.put("price", course.getPrice());
                            courseNode.put("description", course.getDescription());
                            courseNode.put("imgsrc", course.getImgsrc());
                            courseNodes.add(courseNode);
                        }
                        
                        chapterNode.put("courses", courseNodes);
                        chapterNodes.add(chapterNode);
                    }
                    
                    subCategoryNode.put("chapters", chapterNodes);
                    subCategoryNodes.add(subCategoryNode);
                }
                
                categoryNode.put("subCategories", subCategoryNodes);
                categories.add(categoryNode);
            }
        }
        
        courseTree.put("categories", categories);
        return courseTree;
    }
    
    @Override
    public Map<String, Object> getCourseTreeByCategoryId(Integer categoryId) {
        Map<String, Object> courseTree = new HashMap<>();
        
        // 获取课程大类信息
        CourseCategories mainCategory = courseCategoriesMapper.findById(categoryId);
        if (mainCategory == null || mainCategory.getParentCategoryId() != null) {
            return courseTree; // 不是课程大类或不存在
        }
        
        courseTree.put("categoryId", mainCategory.getCategoryId());
        courseTree.put("categoryName", mainCategory.getCategoryName());
        courseTree.put("imgSrc", mainCategory.getImgSrc());
        
        // 获取该课程大类下的课程小类
        List<CourseCategories> subCategories = courseCategoriesMapper.findSubCategoriesByParentId(categoryId);
        List<Map<String, Object>> subCategoryNodes = new ArrayList<>();
        
        for (CourseCategories subCategory : subCategories) {
            Map<String, Object> subCategoryNode = new HashMap<>();
            subCategoryNode.put("categoryId", subCategory.getCategoryId());
            subCategoryNode.put("categoryName", subCategory.getCategoryName());
            subCategoryNode.put("imgSrc", subCategory.getImgSrc());
            
            // 获取该课程小类下的章节
            List<CourseChapters> chapters = courseChaptersService.getChaptersBySubCategoryId(subCategory.getCategoryId());
            List<Map<String, Object>> chapterNodes = new ArrayList<>();
            
            for (CourseChapters chapter : chapters) {
                Map<String, Object> chapterNode = new HashMap<>();
                chapterNode.put("chapterId", chapter.getChapterId());
                chapterNode.put("chapterName", chapter.getChapterName());
                chapterNode.put("isFree", chapter.getIsFree());
                
                // 获取该章节下的具体课程
                List<Courses> courses = coursesService.getCoursesByChapterId(chapter.getChapterId());
                List<Map<String, Object>> courseNodes = new ArrayList<>();
                
                for (Courses course : courses) {
                    Map<String, Object> courseNode = new HashMap<>();
                    courseNode.put("courseId", course.getCourseId());
                    courseNode.put("courseName", course.getCourseName());
                    courseNode.put("courseType", course.getCourseType());
                    courseNode.put("price", course.getPrice());
                    courseNode.put("description", course.getDescription());
                    courseNode.put("imgsrc", course.getImgsrc());
                    courseNodes.add(courseNode);
                }
                
                chapterNode.put("courses", courseNodes);
                chapterNodes.add(chapterNode);
            }
            
            subCategoryNode.put("chapters", chapterNodes);
            subCategoryNodes.add(subCategoryNode);
        }
        
        courseTree.put("subCategories", subCategoryNodes);
        return courseTree;
    }
    
    @Override
    public List<CourseTreeVO> getCompleteCourseTreeVO() {
        List<CourseTreeVO> courseTreeList = new ArrayList<>();
        
        // 获取所有课程大类（parent_category_id为null）
        List<CourseCategories> mainCategories = courseCategoriesMapper.findAllCategories();
        
        for (CourseCategories mainCategory : mainCategories) {
            if (mainCategory.getParentCategoryId() == null) {
                CourseTreeVO categoryNode = new CourseTreeVO();
                categoryNode.setCategoryId(mainCategory.getCategoryId());
                categoryNode.setCategoryName(mainCategory.getCategoryName());
                categoryNode.setImgSrc(mainCategory.getImgSrc());
                
                // 获取该课程大类下的课程小类
                List<CourseCategories> subCategories = courseCategoriesMapper.findSubCategoriesByParentId(mainCategory.getCategoryId());
                List<CourseTreeVO.SubCategoryVO> subCategoryNodes = new ArrayList<>();
                
                for (CourseCategories subCategory : subCategories) {
                    CourseTreeVO.SubCategoryVO subCategoryNode = new CourseTreeVO.SubCategoryVO();
                    subCategoryNode.setCategoryId(subCategory.getCategoryId());
                    subCategoryNode.setCategoryName(subCategory.getCategoryName());
                    subCategoryNode.setImgSrc(subCategory.getImgSrc());
                    
                    // 获取该课程小类下的章节
                    List<CourseChapters> chapters = courseChaptersService.getChaptersBySubCategoryId(subCategory.getCategoryId());
                    List<CourseTreeVO.ChapterVO> chapterNodes = new ArrayList<>();
                    
                    for (CourseChapters chapter : chapters) {
                        CourseTreeVO.ChapterVO chapterNode = new CourseTreeVO.ChapterVO();
                        chapterNode.setChapterId(chapter.getChapterId());
                        chapterNode.setChapterName(chapter.getChapterName());
                        chapterNode.setIsFree(chapter.getIsFree());
                        
                        // 获取该章节下的具体课程
                        List<Courses> courses = coursesService.getCoursesByChapterId(chapter.getChapterId());
                        List<CourseTreeVO.CourseVO> courseNodes = new ArrayList<>();
                        
                        for (Courses course : courses) {
                            CourseTreeVO.CourseVO courseNode = new CourseTreeVO.CourseVO();
                            courseNode.setCourseId(course.getCourseId());
                            courseNode.setCourseName(course.getCourseName());
                            courseNode.setCourseType(course.getCourseType());
                            courseNode.setPrice(course.getPrice());
                            courseNode.setDescription(course.getDescription());
                            courseNode.setImgsrc(course.getImgsrc());
                            courseNodes.add(courseNode);
                        }
                        
                        chapterNode.setCourses(courseNodes);
                        chapterNodes.add(chapterNode);
                    }
                    
                    subCategoryNode.setChapters(chapterNodes);
                    subCategoryNodes.add(subCategoryNode);
                }
                
                categoryNode.setSubCategories(subCategoryNodes);
                courseTreeList.add(categoryNode);
            }
        }
        
        return courseTreeList;
    }
    
    @Override
    public CourseTreeVO getCourseTreeVOByCategoryId(Integer categoryId) {
        // 获取指定的课程大类
        CourseCategories mainCategory = courseCategoriesMapper.findById(categoryId);
        if (mainCategory == null || mainCategory.getParentCategoryId() != null) {
            return null; // 不是顶级分类或不存在
        }
        
        CourseTreeVO categoryNode = new CourseTreeVO();
        categoryNode.setCategoryId(mainCategory.getCategoryId());
        categoryNode.setCategoryName(mainCategory.getCategoryName());
        categoryNode.setImgSrc(mainCategory.getImgSrc());
        
        // 获取该课程大类下的课程小类
        List<CourseCategories> subCategories = courseCategoriesMapper.findSubCategoriesByParentId(mainCategory.getCategoryId());
        List<CourseTreeVO.SubCategoryVO> subCategoryNodes = new ArrayList<>();
        
        for (CourseCategories subCategory : subCategories) {
            CourseTreeVO.SubCategoryVO subCategoryNode = new CourseTreeVO.SubCategoryVO();
            subCategoryNode.setCategoryId(subCategory.getCategoryId());
            subCategoryNode.setCategoryName(subCategory.getCategoryName());
            subCategoryNode.setImgSrc(subCategory.getImgSrc());
            
            // 获取该课程小类下的章节
            List<CourseChapters> chapters = courseChaptersService.getChaptersBySubCategoryId(subCategory.getCategoryId());
            List<CourseTreeVO.ChapterVO> chapterNodes = new ArrayList<>();
            
            for (CourseChapters chapter : chapters) {
                CourseTreeVO.ChapterVO chapterNode = new CourseTreeVO.ChapterVO();
                chapterNode.setChapterId(chapter.getChapterId());
                chapterNode.setChapterName(chapter.getChapterName());
                chapterNode.setIsFree(chapter.getIsFree());
                
                // 获取该章节下的具体课程
                List<Courses> courses = coursesService.getCoursesByChapterId(chapter.getChapterId());
                List<CourseTreeVO.CourseVO> courseNodes = new ArrayList<>();
                
                for (Courses course : courses) {
                    CourseTreeVO.CourseVO courseNode = new CourseTreeVO.CourseVO();
                    courseNode.setCourseId(course.getCourseId());
                    courseNode.setCourseName(course.getCourseName());
                    courseNode.setCourseType(course.getCourseType());
                    courseNode.setPrice(course.getPrice());
                    courseNode.setDescription(course.getDescription());
                    courseNode.setImgsrc(course.getImgsrc());
                    courseNodes.add(courseNode);
                }
                
                chapterNode.setCourses(courseNodes);
                chapterNodes.add(chapterNode);
            }
            
            subCategoryNode.setChapters(chapterNodes);
            subCategoryNodes.add(subCategoryNode);
        }
        
        categoryNode.setSubCategories(subCategoryNodes);
        return categoryNode;
    }

    @Override
    public List<Courses> getClazzCenter() {
        return courseCategoriesMapper.classcenter();
    }

    @Override
    public Integer findCategoryIdByName(String categoryName) {
        return courseCategoriesMapper.findCategoryIdByName(categoryName);
    }
} 